<template>
	<view>
		<view class="welcome">
			<image style=" width: 100%;object-fit: contain;height: 100%;"
				src="cloud://cu4oral6-9g3l2orjbb347b4c.6375-cu4oral6-9g3l2orjbb347b4c-1307874055/R-C.gif"
				mode="aspectFit"></image>
		</view>
		<view class="btn" style="padding: 5%;">
			<view class="agreement" style="margin-bottom: 20rpx;">
				<van-row>
					<van-col>
						<view class="" @click="onChange">
							<van-radio-group :value="radio">
								<van-radio name="1"></van-radio>
							</van-radio-group>
						</view>

					</van-col>
					<van-col>
						<text>已阅读并同意<text style="color: #EA6000;">法律声明及隐私政策</text></text>
					</van-col>
				</van-row>


			</view>
			<van-button @click="login" v-if="radio==1" type="primary" icon="chat-o" block>微信用户一键登录</van-button>
			<van-button v-else disabled type="primary" icon="chat-o" block>微信用户一键登录</van-button>
		</view>

	</view>
</template>

<script>
	import Page from '../../common/page';
	export default {
		data() {
			return {
				radio: '0',
				userInfo: null,
				token: ""
			}
		},
		methods: {
			onChange() {
				this.$data.radio = this.$data.radio == 1 ? '0' : '1';
			},
			login() {
				/**获取用户信息*/
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res1) => {
						console.log(res1)
						this.userInfo = res1.userInfo

						/**连接服务器接口*/
						wx.login({
							success: res => {
								console.log(res.code)
								// this.code=res.code;
								uni.request({
									url: this.apiurl + '/user/login', //仅为示例，并非真实接口地址。
									data: {
										code: res.code
									},
									header: {
										'custom-header': 'hello' //自定义请求头信息
									},
									success: (res) => {
										// console.log(typeof(res.data.data) );
										this.token = res.data.data;
										this.mySocket();
										// 存储token与用户info
										try {
											console.log(this.token, this.userInfo)
											uni.setStorageSync('token', this.token);
											uni.setStorageSync('userInfo', this
												.userInfo);
										} catch (e) {
											// error
											console.log(e)
										}

										uni.switchTab({
											url: '/pages/index/index'
										});

									}
								});
							}
						})



					},
					fail: (err) => {
						console.log(err)
					}
				})


				// uni.setStorage({
				//     key: 'token',
				//     data: this.token,
				//     success () {
				//         console.log('success');
				//     }
				// });

			}
		}
	}
</script>

<style>
	.welcome {
		width: 100%;
		/* display: inline-block; */
		transition: 0.3s;
		/* background-image: url("https://img.yzcdn.cn/vant/cat.jpeg"); */
		height: 600rpx;
		background-size: cover;
	}
</style>
